/**
 * 
 */
package com.dream.house.controller;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.servyou.utils.CoreUtils;
import cn.com.servyou.utils.Page;

import com.dream.house.entity.BuildDTO;
import com.dream.house.entity.GroupDTO;
import com.dream.house.entity.RentDTO;
import com.dream.house.entity.RoomDTO;
import com.dream.house.entity.UserDTO;
import com.dream.house.service.BuildService;
import com.dream.house.service.RentService;
import com.dream.house.service.RoomService;
import com.dream.house.service.UserService;
import com.dream.house.utils.Constants;

/**
 * @author maoq
 *
 */
@Controller
@RequestMapping("rent")
public class RentController {
	
	@Autowired
	BuildService buildService;
	@Autowired
	UserService userService;
	@Autowired
	RoomService roomService;
	@Autowired
	RentService rentService;

	@RequestMapping("gotozufangdengji")
	public String gotogoufangdengji(Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		List<BuildDTO> buildList = getBuildListByGroupId(groupList.get(0).getGroup_id());
		model.addAttribute("buildList", buildList);
		if(buildList.isEmpty()) {
			model.addAttribute("roomList", Collections.EMPTY_LIST);
		} else {
			model.addAttribute("roomList", getRoomListByBuildId(buildList.get(0).getBuild_id()));
		}
		return "rent/zufang_dengji";
	}
	
	private List<BuildDTO> getBuildListByGroupId(String id) {
		BuildDTO dto = new BuildDTO();
		dto.setGroup_id(id);
		dto.setCheck_status(Constants.CHECK_STATUS_PASSED);
		List<BuildDTO> builds =  buildService.findBuilds(dto);
		return builds;
	}
	
	private List<RoomDTO> getRoomListByBuildId(String id) {
		RoomDTO room = new RoomDTO();
		room.setBuild_id(id);
		room.setCheck_status(Constants.CHECK_STATUS_PASSED);
		return roomService.findRoomsWithBuyRent(room);
	}
	
	@RequestMapping("zufangdengji")
	public String zufangdengji(Model model, HttpSession session, RentDTO dto) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		dto.setCreate_user(user.getUserId());
		try {
			rentService.saveRent(dto);
			model.addAttribute("error", "1");
		} catch (Exception e) {
			model.addAttribute("error", "0");
		}
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		List<BuildDTO> buildList = getBuildListByGroupId(groupList.get(0).getGroup_id());
		model.addAttribute("buildList", buildList);
		if(buildList.isEmpty()) {
			model.addAttribute("roomList", Collections.EMPTY_LIST);
		} else {
			model.addAttribute("roomList", getRoomListByBuildId(buildList.get(0).getBuild_id()));
		}
		return "rent/zufang_dengji";
	}
	
	@RequestMapping("gotozufangchaxun")
	public String gotozufangchaxun(Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		List<BuildDTO> buildList = getBuildListByGroupId(groupList.get(0).getGroup_id());
		model.addAttribute("buildList", buildList);
		if(buildList.isEmpty()) {
			model.addAttribute("roomList", Collections.EMPTY_LIST);
		} else {
			model.addAttribute("roomList", getRoomListByBuildId(buildList.get(0).getBuild_id()));
		}
		return "rent/zufang_chaxun";
	}
	
	@RequestMapping("zufangquery")
	public String zufangquery(Model model, @ModelAttribute("dto")RentDTO dto, int pageNo, int pageSize, String flag) {
		if ("shenpi".equals(flag)) {
			dto.setCheck_status(Constants.CHECK_STATUS_INIT);
		}
		Map<String, Object> condition = CoreUtils.entity2Map(dto);
		Page page = rentService.findByPage(condition, pageNo, pageSize);
		model.addAttribute("page", page);

		if ("shenpi".equals(flag)) {
			return "shenpi/zufang_list";
		}
		
		return "rent/zufang_list";
	}
	
	@RequestMapping("gotoedit/{id}")
	public String gotoedit(@PathVariable String id, Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		List<BuildDTO> buildList = getBuildListByGroupId(groupList.get(0).getGroup_id());
		model.addAttribute("buildList", buildList);
		if(buildList.isEmpty()) {
			model.addAttribute("roomList", Collections.EMPTY_LIST);
		} else {
			model.addAttribute("roomList", getRoomListByBuildId(buildList.get(0).getBuild_id()));
		}
		RentDTO rent = rentService.getRent(id);
		model.addAttribute("rent", rent);
		return "rent/zufang_edit";
	}
	
	@RequestMapping("doedit")
	public String gotoedit(@ModelAttribute("rent") RentDTO rent, Model model, HttpSession session) {
		UserDTO user = (UserDTO)session.getAttribute(Constants.CURRENT_USER);
		List<GroupDTO> groupList = userService.getGroups(user.getUserId());
		model.addAttribute("groupList", groupList);
		List<BuildDTO> buildList = getBuildListByGroupId(groupList.get(0).getGroup_id());
		model.addAttribute("buildList", buildList);
		if(buildList.isEmpty()) {
			model.addAttribute("roomList", Collections.EMPTY_LIST);
		} else {
			model.addAttribute("roomList", getRoomListByBuildId(buildList.get(0).getBuild_id()));
		}
		try {
			rentService.updateRent(rent);
			model.addAttribute("error", "1");
		} catch (Exception e) {
			model.addAttribute("error", "0");
		}
		return "rent/zufang_edit";
	}
	
	@ResponseBody
	@RequestMapping("deleterent/{id}")
	public String deletebuy(@PathVariable String id, Model model) {
		try {
			rentService.removeRent(id);
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}
	
}
